//
//  CYMineViewController.m
//  cyydt
//
//  Created by chu on 2019/1/14.
//  Copyright © 2019 chu. All rights reserved.
//

#import "CYMineViewController.h"
#import "HPMineTableViewCell.h"
#import "CYMineHeaderView.h"

#import "CYAccountSetViewController.h"//个人资料
#import "CYFeedbackListViewController.h"//意见反馈
#import "CYMyQuestionViewController.h"//我的提问
#import "CYWorksManagementViewController.h"//作品管理
#import "CYMessageCenterViewController.h"//我的消息
#import "CYMineWoDeHuiDaViewController.h"//我的回答
#import "CYMyCommentViewController.h"//我的评论
#import "SystemSettingsViewController.h"//系统设置
#import "CYContactUsViewController.h"//联系我们
@interface CYMineViewController ()<UITableViewDelegate, UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    NSArray *_titles;
    NSArray *_images;
    NSInteger _messageCount;
    BOOL updateFlag;//是否需要更新
}
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) UIView *footerView;
//相机控制器
@property(nonatomic,strong)UIImagePickerController *imagePickerVC;

@property (nonatomic, strong) CYMineHeaderView *headerView;

@end

@implementation CYMineViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if ([YLUserInfo isLogIn]) {
        [self getMessageCount];
    }else{
        _messageCount = 0;
        [self.tableView reloadData];
    }
    [self reloadTableHeaderView];
    
    [[AppDelegate sharedAppDelegate] getUserInfosWithComplecation:^{
        [self reloadTableHeaderView];

    }];
    //设置导航栏透明
    [self.navigationController.navigationBar setTranslucent:true];
    //把背景设为空
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    //处理导航栏有条线的问题
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor], NSFontAttributeName : [UIFont systemFontOfSize:17]};
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setTranslucent:NO];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor blackColor], NSFontAttributeName : [UIFont systemFontOfSize:17]};
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _titles = @[@[@"个人资料", @"作品管理", @"我的消息"], @[@"我的提问", @"我的回答", @"我的评论"], @[@"联系我们", @"意见反馈", @"系统设置"]];
    _images = @[@[@"wd_grzl", @"wd_zpgl", @"wd_wdxx"], @[@"wd_wdtw", @"wd_wdwd", @"wd_wdpl"], @[@"wd_lxwm", @"wd_yjfk", @"wd_xtsz"]];
    self.title = @"我的";
    [self.view addSubview:self.tableView];
    if (@available(iOS 11.0, *)) {
        self.tableView.estimatedSectionFooterHeight = 0;
        self.tableView.estimatedSectionHeaderHeight = 0;
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else{
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HPMineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HPMineTableViewCell"];
    if (!cell) {
        cell = [[NSBundle mainBundle] loadNibNamed:@"HPMineTableViewCell" owner:self options:nil][0];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSArray *imagesSec = _images[indexPath.section];
    if (imagesSec.count - 1 == indexPath.row) {
        cell.lineView.hidden = YES;
    }else{
        cell.lineView.hidden = NO;
    }
    if (indexPath.section == 0 && indexPath.row == 2 && _messageCount > 0) {
        [cell.statusBtn setTitle:[NSString stringWithFormat:@"%ld",_messageCount] forState:UIControlStateNormal];
        cell.statusBtn.hidden = NO;
    }else{
        cell.statusBtn.hidden = YES;
    }
    cell.headImageView.image = [UIImage imageNamed:_images[indexPath.section][indexPath.row]];
    cell.contentLabel.text = _titles[indexPath.section][indexPath.row];
    cell.updateLabel.hidden = YES;

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0001f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (![YLUserInfo isLogIn]) {
            LoginViewController *login = [[LoginViewController alloc] init];
            [[AppDelegate sharedAppDelegate] pushViewController:login];
            return;
        }
        if (indexPath.row == 0) {
            CYAccountSetViewController *account = [[CYAccountSetViewController alloc] init];
            [[AppDelegate sharedAppDelegate] pushViewController:account];
        }
        if (indexPath.row == 1) {
            //作品管理
            CYWorksManagementViewController *account = [[CYWorksManagementViewController alloc] init];
            [[AppDelegate sharedAppDelegate] pushViewController:account];
        }
        if (indexPath.row == 2) {
            //我的消息
            CYMessageCenterViewController *account = [[CYMessageCenterViewController alloc] init];
            [[AppDelegate sharedAppDelegate] pushViewController:account];
        }
    }else if (indexPath.section == 1){
        if (![YLUserInfo isLogIn]) {
            LoginViewController *login = [[LoginViewController alloc] init];
            [[AppDelegate sharedAppDelegate] pushViewController:login];
            return;
        }
        if (indexPath.row == 0) {
            //我的提问
            CYMyQuestionViewController *question = [[CYMyQuestionViewController alloc] init];
            [[AppDelegate sharedAppDelegate] pushViewController:question];
        }
        if (indexPath.row == 1) {
            //我的回答
            CYMineWoDeHuiDaViewController *question = [[CYMineWoDeHuiDaViewController alloc] init];
            [[AppDelegate sharedAppDelegate] pushViewController:question];
        }
        if (indexPath.row == 2) {
            //我的回答
            CYMyCommentViewController *question = [[CYMyCommentViewController alloc] init];
            [[AppDelegate sharedAppDelegate] pushViewController:question];
        }
        
    }else{
        if (indexPath.row == 1) {
            CYFeedbackListViewController *feed = [[CYFeedbackListViewController alloc] init];
            [[AppDelegate sharedAppDelegate] pushViewController:feed];
        }else if(indexPath.row == 0){
            CYContactUsViewController *feed = [[CYContactUsViewController alloc] init];
            [[AppDelegate sharedAppDelegate] pushViewController:feed];
        }else{
            SystemSettingsViewController *set = [[SystemSettingsViewController alloc] init];
            [[AppDelegate sharedAppDelegate] pushViewController:set];
        }
    }
}

- (void)reloadTableHeaderView{
    self.headerView.isLogin = [YLUserInfo isLogIn];
    if ([YLUserInfo isLogIn]) {
        [self.headerView.headImageView sd_setImageWithURL:[NSURL URLWithString:[YLUserInfo shareUserInfo].avatar] placeholderImage:[UIImage imageWithColor:[UIColor lightGrayColor] Size:self.headerView.headImageView.frame.size]];
        self.headerView.nicknameLabel.text = [YLUserInfo shareUserInfo].nickname;
        self.headerView.miaoshuLabel.text = [YLUserInfo shareUserInfo].yldescription;
    }
    
}

- (void)getMessageCount{
    NSString *url = [NSString stringWithFormat:@"%@%@",User_HOST, @"message/count"];
    [[XBRequest sharedInstance] getDataWithUrl:url Parameter:nil ResponseObject:^(NSDictionary *responseResult) {
        NSLog(@"未读消息 ---- %@",responseResult);
        if ([responseResult objectForKey:@"resError"]) {
        }else{
            if ([responseResult[Response_code] integerValue] == Response_value) {
                NSDictionary *data = responseResult[@"data"];
                NSNumber *inform = data[@"inform"];
                NSNumber *choose = data[@"choose"];
                NSNumber *notice = data[@"notice"];
                self->_messageCount = [inform integerValue] + [choose integerValue] + [notice integerValue];
                [self.tableView reloadData];
            }else{

            }
        }
    }];
}

- (UITableView *)tableView{
    if (!_tableView) {
        if (@available(iOS 11.0, *)) {
            _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWindowW, kWindowH - Height_TabBar) style:UITableViewStyleGrouped];

        }else{
            _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -Height_NavBar, kWindowW, kWindowH) style:UITableViewStyleGrouped];
        }
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = BackGroundColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerNib:[UINib nibWithNibName:@"HPMineTableViewCell" bundle:nil] forCellReuseIdentifier:@"HPMineTableViewCell"];
        
        _tableView.tableHeaderView = self.headerView;
        _tableView.bounces = NO;
    }
    return _tableView;
}

- (CYMineHeaderView *)headerView{
    if (!_headerView) {
        _headerView = [[CYMineHeaderView alloc] initWithFrame:CGRectMake(0, 0, kWindowW, 260 + Height_NavBar)];
        _headerView.isLogin = NO;
        _headerView.headImageView.image = [UIImage imageWithColor:[UIColor blueColor] Size:CGSizeMake(50, 50)];
        _headerView.nicknameLabel.text = @"123";
        _headerView.miaoshuLabel.text = @"123";
        
    }
    return _headerView;
}

@end
